package com.li.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.DTO.RoomNumberDTO;
import com.li.VO.PageListVO;
import com.li.VO.RoomNumberVO;
import com.li.mapper.RoomNumberMapper;
import com.li.pojo.Host;
import com.li.pojo.RoomNumber;
import com.li.service.HostService;
import com.li.service.RoomNumberService;
import com.li.util.BeanContent;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhiqingLi
 * @Description:
 * @Date: Created in 15:37 2024/3/13
 **/
@Service
public class RoomNumberServiceImpl implements RoomNumberService {

    @Autowired
    private RoomNumberMapper roomNumberMapper;
    @Autowired
    private HostService hostService;

    @Override
    public PageListVO showList(RoomNumberDTO roomNumberDTO) {
        PageHelper.startPage(roomNumberDTO.getPageIndex(), roomNumberDTO.getPageSize());
        RoomNumber roomNumber = new RoomNumber();
        BeanUtils.copyProperties(roomNumberDTO, roomNumber);
        roomNumber.setCompanyId(BeanContent.getCurrentId());
        String mb = roomNumberDTO.getMb();
        if (mb != null && mb != "") {
            String[] split = mb.split("-");
            roomNumber.setAreaU(Integer.valueOf(split[0]));
            roomNumber.setAreaB(Integer.valueOf(split[1]));
        }
        Page<RoomNumber> page = roomNumberMapper.showList(roomNumber);
        return new PageListVO(page.getTotal(), page.getResult());
    }

    @Override
    public void addSubregion(RoomNumber roomNumber) {
        if (roomNumber.getHostId() == null || roomNumber.getHostId() == null) {
            List<Host> hosts = hostService.showNameByHostName(roomNumber.getHostName());
            if (hosts.size() > 0) {
                roomNumber.setPhone(hosts.get(0).getPhone());
                roomNumber.setHostId(hosts.get(0).getHostId());
            }
        }
        roomNumber.setCompanyId(BeanContent.getCurrentId());
        roomNumberMapper.addSubregion(roomNumber);
    }

    @Override
    public RoomNumber updGetById(Long id) {
        return roomNumberMapper.updGetById(id);
    }

    @Override
    public void updSubregion(RoomNumber roomNumber) {
        List<Host> hosts = hostService.showNameByHostName(roomNumber.getHostName());
        if (hosts.size() > 0) {
            roomNumber.setPhone(hosts.get(0).getPhone());
            roomNumber.setHostId(hosts.get(0).getHostId());
        }
        roomNumberMapper.updSubregion(roomNumber);
    }

    //回显房号 单元-房间号
    @Override
    public List<RoomNumberVO> showNumber() {
        List<RoomNumberVO> stringList = roomNumberMapper.showNumber();
        return null;
    }
}
